#include "10_22_prac_CircularQueue.h"
#include <iostream>
using namespace std;

CQ* CircularQueueCreate(int k)
{
	CQ* cq = (CQ*)malloc(sizeof(CircularQueue));
	if (cq == NULL)
	{
		exit(-1);
	}
	cq->a = (int*)malloc(sizeof(int) * (k + 1));
	cq->front = cq->tail = 0;
	cq->k = k;
	return cq;
}
void CircularQueueCreate(CQ* cq,int k)
{
	cq->a = (int*)malloc(sizeof(int) * (k + 1));
	cq->tail = cq->front = 0;
	cq->k = k;
}
bool CircularQueueEmpty(CQ* cq)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if (cq->front == cq->tail)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CircularQueueFull(CQ* cq)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if ((cq->tail + 1) % (cq->k + 1) == cq->front)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void CircularQueueInsert(CQ* cq, int x)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if (CircularQueueFull(cq))
	{
		exit(-1);
	}
	cq->a[cq->tail] = x;
	if (cq->tail == cq->k)
	{
		cq->tail = 0;
	}
	else
	{
		cq->tail++;
	}
	
	
}
void CircularQueueDel(CQ* cq)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if (CircularQueueEmpty(cq))
	{
		exit(-1);
	}
	cq->front++;
	cq->front %= (cq->k + 1);
	
}
int CricularQueueFront(CQ* cq)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if (CircularQueueEmpty(cq))
	{
		return -1;
	}
	return cq->a[cq->front];
}

int CricularQueueRear(CQ* cq)
{
	if (cq == NULL)
	{
		exit(-1);
	}
	if (CircularQueueEmpty(cq))
	{
		return -1;
	}
	if (cq->tail == 0)
	{
		return cq->a[cq->k];
	}
	else
	{
		return cq->a[cq->tail - 1];
	}
	
}